package com.aispeech.testcustom.view;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.Toast;

import com.aispeech.testcustom.R;
import com.aispeech.testcustom.TiApplication;
import com.aispeech.testcustom.utils.PreferenceUtil;
import com.aispeech.testcustom.utils.TiLog;
import com.aispeech.testcustom.utils.UiUtils;

public class SettingsActivity extends AppCompatActivity {

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            SettingsActivity.this.finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        UiUtils.showBack4ActionBar(this, "设置");

        if (null == savedInstanceState) {
//            SettingFragment settingFragment = new SettingFragment();
            getFragmentManager().beginTransaction().replace(android.R.id.content, new
                    SettingFragment()).commit();
        }
    }


    public static class SettingFragment extends PreferenceFragment implements SharedPreferences
            .OnSharedPreferenceChangeListener {
        private ListPreference lp;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.launch_settings);

            lp = (ListPreference) findPreference("settings_log_list");
            lp.setSummary(lp.getEntry());
        }

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            if (key.equals("init_aios_sdk_in_application")) {
                //是否在application中初始化语音
                TiLog.d("isInitInApplication:" + sharedPreferences.getBoolean(key, true));
                Toast.makeText(TiApplication.getContext(), "应用重启后生效!!!", Toast.LENGTH_SHORT).show();
            } else if (key.equals("settings_log_list")) {
                //日志等级设置
                lp.setSummary(lp.getValue());
                TiLog.setLoglevel(Integer.parseInt(lp.getValue()));
            } else if(key.equals("is_show_splash")) {
                //是否显示引导界面

            }else if (key.equals("save_original_mswake_audio")) {
                PreferenceUtil.setString(TiApplication.getContext(),PreferenceUtil.SAVE_ORIGINAL_MSWAKE_AUDIO,sharedPreferences.getString(key,"20"));
            } else if (key.equals("save_aec_mswake_audio")) {
                PreferenceUtil.setString(TiApplication.getContext(),PreferenceUtil.SAVE_AEC_MSWAKE_AUDIO,sharedPreferences.getString(key,"60"));
            }
        }

        @Override
        public void onResume() {
            super.onResume();
            // Set up a listener whenever a key changes
            getPreferenceScreen().getSharedPreferences()
                    .registerOnSharedPreferenceChangeListener(this);
        }

        @Override
        public void onPause() {
            super.onPause();
            // Unregister the listener whenever a key changes
            getPreferenceScreen().getSharedPreferences()
                    .unregisterOnSharedPreferenceChangeListener(this);
        }
    }
}
